package com.addcn.pushlibrary.huawei
import android.app.Application
import android.content.Context
import android.text.TextUtils
import android.util.Log
import com.addcn.pushlibrary.LConfiguration
import com.addcn.pushlibrary.utils.catchs.LSharedUtil
import com.huawei.agconnect.config.AGConnectServicesConfig
import com.huawei.hms.aaid.HmsInstanceId
import com.huawei.hms.common.ApiException
import com.huawei.hms.push.HmsMessaging


/**
 * author:luwenbo
 * created on date : 2019-06-06
 * content:
 * describe:
 */

class LHuaweiManger{
    companion object{
        var TAG = "LHuaweiManger"
        fun init(application: Application, configuration: LConfiguration?) {
            object : Thread() {
                override fun run() {
                    try {
                        val appId =
                            AGConnectServicesConfig.fromContext(application.applicationContext)
                                .getString("client/app_id")
                        Log.i(TAG, "appId:$appId")
                        val token =
                            HmsInstanceId.getInstance(application.applicationContext).getToken(appId, "HCM")
                        Log.i(TAG, "get token:$token")
                        if (!TextUtils.isEmpty(token)) {
                            //sendRegTokenToServer(token)
                            LSharedUtil.putString(application.applicationContext, "l_huawei_token",token)
                        }
                        //showLog("get token:$token")
                    } catch (e: ApiException) {
                        Log.e(TAG, "get token failed, $e")
                        //showLog("get token failed, $e")
                    }
                }
            }.start()
        }

        fun startPush(context: Context) {
            HmsMessaging.getInstance(context).turnOnPush().addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    //showLog("turnOnPush Complete")
                } else {
                    //showLog("turnOnPush failed: cause=" + task.exception.message)
                }
            }

        }

        fun stopPush(context: Context) {
            HmsMessaging.getInstance(context).turnOffPush().addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    //showLog("turnOffPush Complete")
                } else {
                    //showLog("turnOffPush  failed: cause =" + task.exception.message)
                }
            }
        }

        fun setTag(context: Context){
            //HMSAgent.Push.
        }
    }



}